package com.yike.common.config;

import com.yike.common.request.lkl.LklAuthHeader;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AuthHeaderParser {
    // 示例输入："RSA timestamp="123456789",nonce_str="abcdef",signature="xyz123""

    private static final Pattern PATTERN = Pattern.compile(
            "\\s*(\\w+)\\s+" +
                    "timestamp=\"([^\"]*)\"," +
                    "nonce_str=\"([^\"]*)\"," +
                    "signature=\"([^\"]*)\""
    );

    public static LklAuthHeader parse(String authString) {
        Matcher matcher = PATTERN.matcher(authString);
        if (matcher.find()) {
            return new LklAuthHeader(
                    matcher.group(1),  // algorithm
                    matcher.group(2),  // timestamp
                    matcher.group(3),  // nonce_str
                    matcher.group(4)   // signature
            );
        }
        throw new IllegalArgumentException("Invalid auth format");
    }
}
